<?php


    if (!defined('EXPONENT')) exit('');
    global $db;

    if (exponent_permissions_check(array("administrate", "mail"),$loc)) {
        // nov 6, 20110 - trungdt - add send type
        $send_type=0;
        // =0: to all who registered
        // =1: to all normal users
        // =2: to all users
        //= 3:Register(not user)
        //= 4:Register(both user and non user)
        // = 5:Customer Select
        if (isset($_REQUEST['send_type']))
            $send_type = intval($_REQUEST['send_type']);
        $emails_temp=array();
        if($send_type == 3)
        {
            $sql_where="users_id=0";
            $emails_temp=$db->selectObjects("maillinglist",$sql_where);
        }
        else if($send_type == 4)
            {
                $sql_where="users_id=0 or users_id>0";
                $emails_temp=$db->selectObjects("maillinglist",$sql_where);
            }
            else if ($send_type == 0)
                {
                    $sql_where="blacklist=0";
                    $emails_temp=$db->selectObjects("maillinglist",$sql_where);
                }
                else 
                {
                    $sql_where="is_admin = 0 AND is_acting_admin=0";
                    if ($send_type == 2) // all users
                        $sql_where="";
                    $emails_temp=$db->selectObjects("user", $sql_where);
        }


        if (count($emails_temp) == 0)
            exponent_flow_redirect(); 
        $headers = array();
        $headers['From'] =  SMTP_FROMADDRESS ;
        $headers['Reply-to'] = SMTP_FROMADDRESS;

        // fix href html content
        $content=$_REQUEST['content'];
        $content=str_replace('\"','"', $content);
        $content=str_replace("\'","'", $content);
        $content=str_replace('src="','src="' . URL_FULL,$content);
        $content=str_replace("src='","src='" . URL_FULL,$content);
        $content=str_replace('href="','href="' . URL_FULL,$content);
        $content=str_replace("href='","href='" . URL_FULL,$content);

        require_once(BASE.'subsystems/mail.php');
        $emails=array();
        // Nov 6, 2011 - trungdt - Add  %name% for Name (replace)
        $bk_content=$content;
        foreach ($emails_temp as $k=>$v)
        {
            if ($send_type >0)
                $content=str_replace("%name%", "{$v->firstname} {$v->lastname}", $bk_content);


        }    
        exponent_flow_redirect();
    }
    else
    {
        echo SITE_404_HTML;    
    }
?>
